<?php 

function sndvivill_add_partytext(){
	return drupal_get_form('sndvivill_partytext_form');
}

function sndvivill_edit_partytext($partytext_id){
	if(is_numeric($partytext_id) ){
		return drupal_get_form('sndvivill_partytext_form');
	}else{
		return t('error').": $partytext_id ".t('is not valid');
	}
}

function sndvivill_partytext_form_submit($form, &$form_state){
	db_set_active(VIVILL_DB);
	
	if(is_numeric($form_state['values']['partytext_id'])){
		db_query("UPDATE partytext SET title = '%s', year = %d, party_id = %d, type = '%s', text_sv = '%s', html_sv = '%s', label_sv = '%s', suffix = '%s' 
																			 WHERE party_id = %d", $form_state['values']['label'], 
																								   $form_state['values']['year'],
																								   $form_state['values']['party_id'],
																								   $form_state['values']['type'],
																								   $form_state['values']['text_sv'],
																								   $form_state['values']['html_sv'],
																								   $form_state['values']['label_sv'],
																								   $form_state['values']['suffix'],
																								   $form_state['values']['party_id']
																								   );
		db_set_active('default');
		drupal_set_message(t('Updated').' '.$form_state['values']['title']);
	}else{
		db_query("INSERT INTO party (title, year, party_id, type, text_sv, html_sv, label_sv, suffix) VALUES('%s', %d, %d, '%s', '%s', '%s', '%s', '%s')", 
																						$form_state['values']['title'], 
																						$form_state['values']['year'],
																						$form_state['values']['party_id'], 
																						$form_state['values']['type'],
																						$form_state['values']['text_sv'], 
																						$form_state['values']['html_sv'],
																						$form_state['values']['label_sv'],
																						$form_state['values']['suffix']);
		db_set_active('default');
		drupal_set_message(t('Added').' '.$form_state['values']['name_sv']);
	}
	
	db_set_active('default');
}

function sndvivill_partytext_form(){
	db_set_active(VIVILL_DB);
	$result = db_query("SELECT party_id, label, name_sv FROM party");
	$partys = array();
	while($row = db_fetch_object($result)){
		$partys[$row->party_id] = $row->label.' -'.$row->name_sv;
	}
	
	$default = array();
	if(is_numeric(arg(2))){
		$result = db_query("SELECT * FROM partytext WHERE partytext_id = %d", arg(2));	
		$default = db_fetch_array($result);	
	}
	
	db_set_active('default');
	
	$form = array();
	$form['title'] = array(
	  '#weight' => '0',
	  '#size' => '50',
	  '#title' => t('Title'),
	  '#type' => 'textfield',
	  '#default_value' => $default['title_sv'],
	);
	$form['year'] = array(
	  '#weight' => '1',
	  '#required' => '1',
	  '#type' => 'textfield',
	  '#title' => 'Year',
	  '#default_value' => $default['year'],
	);
	$form['party_id'] = array(
	  '#multiple_toggle' => '1',
	  '#key_type' => 'associative',
	  '#required' => '1',
	  '#weight' => '2',
	  '#default_value' => $default['party_id'],
	  '#type' => 'select',
	  '#title' => t('Party'),
	  '#options' => $partys,
	);
	$form['type'] = array(
	  '#key_type' => 'associative',
	  '#required' => '1',
	  '#weight' => '3',
	  '#default_value' => $default['type'],
	  '#type' => 'radios',
	  '#title' => 'Type',
	  '#options' => array(
	    'manifesto' => 'manifesto',
	    'program' => 'program',
	  ),
	);
	$form['text_sv'] = array(
	  '#required' => '1',
	  '#weight' => '4',
	  '#title' => 'Text sv',
	  '#type' => 'textarea',
	   '#default_value' => $default['text_sv'],
	);
	$form['html_sv'] = array(
	  '#weight' => '5',
	  '#required' => '1',
	  '#type' => 'textarea',
	  '#title' => 'Html sv',
	  '#default_value' => $default['html_sv'],
	);
	$form['label_sv'] = array(
	  '#weight' => '6',
	  '#required' => '1',
	  '#type' => 'textfield',
	  '#title' => t('label'),
	  '#default_value' => $default['label_sv'],
	);
	$form['suffix'] = array(
	  '#weight' => '7',
	  '#size' => '1',
	  '#type' => 'textfield',
	  '#title' => t('suffix'),
	 '#default_value' => $default['suffix'],
	);
	
	$form['partytext_id'] = array(
		'#type' => 'hidden',
		'#title' => t('Id of the party'),
		'#maxlength' => 256,
		'#required' => FALSE,
		'#weight' => 10,
		'#default_value' => $default['partytext_id'],
	);

	$form['submit'] = array(
		'#weight' => '9',
		'#type' => 'submit',
		'#value' => t('Save'),
	);		
	return $form;	
}


?>